<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$return['ischecklogin']=cfc('core')->checklogin('','',true);
if($_RQ['op']=='get'){
	$setting=uni_setting('pay');
	if(!$setting['credit'] || $setting['norecharge']){
		exi('系统暂不支持充值！','error');
	}
	if($setting['credit2_czlink']){
		$credit2_czlink=links($setting['credit2_czlink']);
		if($credit2_czlink['plugin']!='core' || $credit2_czlink['action']!='member.recharge'){
			exi('','',$setting['credit2_czlink']);
		}
	}
	$return['credit_activitys']=$setting['credit_activitys']?:array();
	$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('credit2'));
	$newcoupons=model('jy_weishop/coupon')->getmycoupon_ing(2,$goodsprice);
	$return['item']['couponid']=$newcoupons[0]['id'];
	$return['coupons']=$newcoupons;
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	$fee=intval($post['money']*100)/100;
	if(!$fee || $fee<=0){
		exi('请先设置充值金额！并且为数字！','error');
	}
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'couponid'=>intval($post['couponid']),
		'fee'=>$fee,
		'wxapp_id'=>$_SESSION['wxapp_id']?:0,
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'余额充值',
		'plugin'=>'core',
		//'pay_type'=>'wechat',
		'no_type'=>array('hdfk','credit'),
		'back_url'=>'core/member.recharge/pay_result/',
		'success_url'=>'core/member.feecard/',
		'error_url'=>'core/member.feecard/',
	);
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	pdo_update('core_recharge',array('pay_id'=>$pay_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$recharge=pdo_get('core_recharge',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('id','wxapp_id','pay_id','fee','status','couponid'));
	if($recharge['fee']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('core_recharge',array('status'=>1,'pay_type'=>$payresult['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
	cfc('core')->member_credit($_SESSION['uid'],'credit2',$recharge['fee'],1,'member','会员自助充值');
	$setting=uni_setting('pay');
	if($setting['credit_activitys']){
		//充值赠送
		$give=0;
		$couponids='';
		foreach($setting['credit_activitys'] as $item){
			if($recharge['fee']>=$item['condition'] && $item['money']>$give){
				$give=$item['money'];
				$couponids=$item['couponids'];
				$sendnum=$item['couponnum'];
				$allday=$item['day'];
			}
		}
		$allday=intval($allday)?:1;
		if($allday>=1 && $give>0){
			$dayfee=$give/$allday;
			pdo_update('core_recharge',array('dayfeetime'=>time(),'dayfee'=>$dayfee,'allday'=>$allday,'lastday'=>$allday-1),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($dayfee>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit2',$dayfee,1,'member',$allday>1?'会员充值分期赠送':'会员充值赠送');
		}
		if($couponids){
			$sendnum=$sendnum?:1;
			$couponids=str_array($couponids);
			foreach($couponids as $couponid){
				for($i=0;$i<$sendnum;$i++){
					model('jy_weishop/coupon')->getcoupon($couponid,$_SESSION['uid'],'',true,'','',3);
				}
			}
		}
	}
	//判断优惠券
	if($recharge['couponid']){
		$couponinfo=pdo_get('jy_weishop_coupon_getlog',array('id'=>$recharge['couponid'],'uniacid'=>$_SESSION['uniacid']));
		$couponinfo['item']=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$couponinfo['couponid']),array('backmoney','backcredit','backredpack'));
		if($couponinfo['item']['backmoney']>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit2',$couponinfo['item']['backmoney'],1,'member','充值券赠送');
		}
		if($couponinfo['item']['backcredit']>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit1',$couponinfo['item']['backcredit'],1,'member','充值券赠送');
		}
		if($couponinfo['item']['backredpack']>0){
			$params=array(
				'money'=>$couponinfo['item']['backredpack'],
				'mid'=>$_SESSION['uid'],
				'remark'=>'充值券赠送',
			);
			cfc('red_pack')->get_money($params);
		}
		pdo_update('jy_weishop_coupon_getlog',array('isuse'=>1),array('id'=>$recharge['couponid'],'uniacid'=>$_SESSION['uniacid']));
	}
	if($setting['credit2_tuiper']>0){
		//充值直推分润
		$fromuser=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		if($fromuser){
			$credit2_tuifee=$setting['credit2_tuiper']*$recharge['fee']/100;
			if($credit2_tuifee>0){
				$red=array(
					'mid'=>$fromuser,
					'money'=>$credit2_tuifee,
					'plugin'=>'core',
					'remark'=>'直推充值分润',
				);
				cfc('red_pack')->get_money($red);
			}
		}
	}
	$recharge['time']=TIMESTAMP;
	publish('core','member.recharge',$recharge);
	if($payresult['pay_type']=='ttpay'){
		//抖音小程序同步订单
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$recharge['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),array('id','tt_appid','tt_paymchid','tt_paysecret'));
		$setting=array(
				'id'=>$wxapp['id'],
				'app_id'=>$wxapp['tt_appid'],
				'tt_paymchid'=>$wxapp['tt_paymchid'],
				'tt_paysecret'=>$wxapp['tt_paysecret'],
		);
		if($wxapp){
			$recharge['list'][]=array(
				'goodsid'=>108,
				'image'=>tomedia('../core/resource/images/welcomehdp02.jpg'),
				'title'=>'会员充值',
				'price'=>$recharge['fee'],
			);
			$recharge['status']=1;
			$recharge['price']=$recharge['fee'];
			$recharge['detail_url']='core/member/credit_log/index';
			$recharge['tid']=pdo_getcolumn('core_paylog',array('id'=>$recharge['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
			cfc('tt_pay',$setting)->push_order($recharge);
		}
	}elseif($payresult['pay_type']=='kspay'){
		//依赖插件订阅同步
	}
	exi('充值成功！');
}